package com.example.demo.service.impl;

import com.example.demo.domain.CombineClause;
import com.example.demo.domain.SingleClause;
import com.example.demo.domain.entity.VojObjectiveQuestion;
import com.example.demo.domain.exam.Paging;
import com.example.demo.domain.exam.QueryModel;
import com.example.demo.domain.exam.condition.VojObjectiveQuestionCondition;
import com.example.demo.mapper.VojObjectiveQuestionMapper;
import com.example.demo.service.VojObjectiveQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class VojObjectiveQuestionServiceImpl extends BaseServiceImpl<VojObjectiveQuestionMapper, VojObjectiveQuestion> implements VojObjectiveQuestionService {

    private final VojObjectiveQuestionMapper mapper;

    @Override
    public List<VojObjectiveQuestion> page(QueryModel<VojObjectiveQuestionCondition> model) {
        Paging paging = Optional.ofNullable(model.getPaging()).orElse(new Paging());
        paging.setPageIndex(paging.getPageIndex() - 1);
        VojObjectiveQuestionCondition condition = Optional.ofNullable(model.getCondition()).orElse(new VojObjectiveQuestionCondition());
        return mapper.page(condition, paging);
    }

    @Override
    public List<VojObjectiveQuestion> getByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return mapper.getByIds(ids);
    }

    @Override
    public List<VojObjectiveQuestion> getByIdsNoAnswer(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return mapper.getByIds(ids);
    }

    @Override
    public List<VojObjectiveQuestion> getByCreatorId(Long creatorId, Long companyId) {
        if (creatorId == null || companyId == null) {
            return Collections.emptyList();
        }
        return selectList(CombineClause.and(
                SingleClause.equal("creator_id", creatorId),
                SingleClause.equal("company_id", companyId)
        ));
    }

}
